package com.example.myanimator.mcanvas

import android.content.Context
import android.graphics.*
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RectShape
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.drawable.toBitmap
import com.example.myanimator.R

/**
 * @author tgw
 * @date 2021/4/6
 * @describe BitmapShader
 */
class CustomShaderView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    var mShapeDrawable: ShapeDrawable? = null
    var mBitmap: Bitmap? = null

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let { mShapeDrawable?.draw(it) }
    }

    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null) //禁用加速
        mShapeDrawable = ShapeDrawable(RectShape())
        var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.aa)
        var bitmapShader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        //因为定义
        mShapeDrawable?.let {
            it.bounds = Rect(300, 50, 600, 500)
//            it.bounds = Rect(0, 0, bitmap.width, bitmap.height)
            it.paint.shader = bitmapShader


        }
    }

}